package com.gcc.myapplicationlab7cluo29.slice;

import com.gcc.myapplicationlab7cluo29.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.sensor.agent.CategoryMotionAgent;
import ohos.sensor.bean.CategoryMotion;
import ohos.sensor.data.CategoryMotionData;
import ohos.sensor.listener.ICategoryMotionDataCallback;

public class MainAbilitySlice extends AbilitySlice {

    private CategoryMotionAgent categoryMotionAgent = new CategoryMotionAgent();

    private ICategoryMotionDataCallback MotionDataCallbackObject;

    private CategoryMotion motionSensor;

    private static final long INTERVAL = 100000000L;

    private CategoryMotionAgent categoryMotionAgent2 = new CategoryMotionAgent();

    private ICategoryMotionDataCallback MotionDataCallbackObject2;

    private CategoryMotion motionSensor2;

    //step counter


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Text text_x = (Text) findComponentById(ResourceTable.Id_text_x);

        Text text_y = (Text) findComponentById(ResourceTable.Id_text_y);

        Text text_z = (Text) findComponentById(ResourceTable.Id_text_z);

        Text text_step = (Text) findComponentById(ResourceTable.Id_text_step);



        // 创建传感器回调对象。
        MotionDataCallbackObject = new ICategoryMotionDataCallback() {
            @Override
            public void onSensorDataModified(CategoryMotionData categoryMotionData) {
                // 传感器数据对象解析和使用
                //int dim = categoryMotionData.getSensorDataDim(); // 获取传感器的维度信息
                float x = categoryMotionData.getValues()[0]; // 获取motion类传感器的第一维数据
                float y = categoryMotionData.getValues()[1]; // 获取 传感器的第2维数据
                float z = categoryMotionData.getValues()[2]; // 获取 传感器的第3维数据

                getUITaskDispatcher().asyncDispatch(()->{
                    text_x.setText(" x =   " + x);
                    text_y.setText(" y =   " + y);
                    text_z.setText(" z =   " + z);
                });

            }

            @Override
            public void onAccuracyDataModified(CategoryMotion categoryOrientation, int index) {
                // 使用变化的精度
            }

            @Override
            public void onCommandCompleted(CategoryMotion categoryOrientation) {
                // 传感器执行命令回调
            }
        };



        // 创建传感器回调对象。 step
        MotionDataCallbackObject2 = new ICategoryMotionDataCallback() {
            @Override
            public void onSensorDataModified(CategoryMotionData categoryMotionData) {
                // 传感器数据对象解析和使用
                //int dim = categoryMotionData.getSensorDataDim(); // 获取传感器的维度信息
                float step = categoryMotionData.getValues()[0]; // 获取motion类传感器的第一维数据

                getUITaskDispatcher().asyncDispatch(()->{
                    text_step.setText(" step =   " + step);

                });

            }

            @Override
            public void onAccuracyDataModified(CategoryMotion categoryOrientation, int index) {
                // 使用变化的精度
            }

            @Override
            public void onCommandCompleted(CategoryMotion categoryOrientation) {
                // 传感器执行命令回调
            }
        };


        motionSensor = categoryMotionAgent.getSingleSensor(
                CategoryMotion.SENSOR_TYPE_ACCELEROMETER);
        if (motionSensor != null) {
            categoryMotionAgent.setSensorDataCallback(
                    MotionDataCallbackObject, motionSensor, INTERVAL);
        }



        motionSensor2 = categoryMotionAgent2.getSingleSensor(
                CategoryMotion.SENSOR_TYPE_PEDOMETER);
        if (motionSensor2 != null) {
            categoryMotionAgent2.setSensorDataCallback(
                    MotionDataCallbackObject2, motionSensor2, INTERVAL);
        }

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
